﻿using System.Linq.PageList;
using Dyao.Book.Application;
using Dyao.Book.Contract;
using Dyao.Book.Core;
using Dyao.Book.Web.Extensions;
using Microsoft.AspNetCore.Mvc;

namespace Dyao.Book.Web.Controllers
{
    public class ChapterMgrController : ApiV1BaseController
    {
        private readonly ChapterMgrApplication _chapterMgrApplication;
        public ChapterMgrController(ChapterMgrApplication chapterMgrApplication)
        {
            _chapterMgrApplication = chapterMgrApplication;
        }

        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="filterModel">过滤模型</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<ApiResult<PageList<ChapterResultDto>>> GetPageListAsync(ChapterFilterDto filterModel, CancellationToken cancellationToken = default)
        {
            var filter = filterModel;
            var pageFilter = filter.ToPageFilter(filter);
            return (PageList<ChapterResultDto>)await _chapterMgrApplication.GetPageListAsync(pageFilter, cancellationToken);
        }

        /// <summary>
        /// 修改信息
        /// </summary>
        /// <param name="chapter">章节信息</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<ApiResult<string>> ModifyAsync(ChapterInfoDto chapter, CancellationToken cancellationToken = default)
        {
            this.ValidateModelState();
            var msg = await _chapterMgrApplication.ModifyAsync(chapter, cancellationToken);
            return msg;
        }

        /// <summary>
        /// 删除信息
        /// </summary>
        /// <param name="id">编号</param>
        /// <param name="cancellationToken">取消令牌</param>
        /// <returns></returns>
        [HttpPost("[action]")]
        public async Task<ApiResult<string>> DeleteAsync(CommonIdDto id, CancellationToken cancellationToken = default)
        {
            this.ValidateModelState();
            var msg = await _chapterMgrApplication.DeleteAsync(id.Id, cancellationToken);
            return msg;
        }
    }
}
